package com.ruoyi.shopping.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.service.impl.SchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.shopping.mapper.CommodityOrderMapper;
import com.ruoyi.shopping.domain.CommodityOrder;
import com.ruoyi.shopping.service.ICommodityOrderService;

import javax.annotation.Resource;

/**
 * 商品订单Service业务层处理
 *
 * @author lfm
 * @date 2024-08-14
 */
@Service
public class CommodityOrderServiceImpl implements ICommodityOrderService {
    @Autowired
    private CommodityOrderMapper commodityOrderMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private SchedulingService schedulingService;

    /**
     * 查询商品订单
     *
     * @param id 商品订单主键
     * @return 商品订单
     */
    @Override
    public CommodityOrder selectCommodityOrderById(Long id) {
        return commodityOrderMapper.selectCommodityOrderById(id);
    }

    /**
     * 查询商品订单列表
     *
     * @param commodityOrder 商品订单
     * @return 商品订单
     */
    @Override
    public List<CommodityOrder> selectCommodityOrderList(CommodityOrder commodityOrder) {

        return commodityOrderMapper.selectCommodityOrderList(commodityOrder);
    }

    /**
     * 新增商品订单
     *
     * @param commodityOrder 商品订单
     * @return 结果
     */
    @Override
    public int insertCommodityOrder(CommodityOrder commodityOrder) {

        commodityOrder.setPayOrNot(0L);
        commodityOrder.setOrderTime(new Date());
        commodityOrder.setCancelOrNot(0L);
        commodityOrder.setGoodsOrNot(0L);
        commodityOrder.setDeliverOrNot(0L);
        int insert = commodityOrderMapper.insertCommodityOrder(commodityOrder);
        //将订单id存入redis
        long timestamp = System.currentTimeMillis();
        redisTemplate.opsForValue().set("commodityOrder" + commodityOrder.getId(), timestamp);
        redisTemplate.expire("commodityOrder" + commodityOrder.getId(), 60, TimeUnit.MINUTES);

        Runnable task = () -> {
            System.out.println("Task executed at " + new Date());
            // 你要执行的任务逻辑
            commodityOrder.setCancelOrNot(1L);
            commodityOrderMapper.updateCommodityOrder(commodityOrder);
        };
        long oneHourInMillis = 60 * 60 * 1000;
        schedulingService.scheduleOneTimeTask(task, oneHourInMillis);
        return insert;
    }

    /**
     * 修改商品订单
     *
     * @param commodityOrder 商品订单
     * @return 结果
     */
    @Override
    public int updateCommodityOrder(CommodityOrder commodityOrder) {

        if(commodityOrder.getPayOrNot()==1&&(commodityOrder.getDeliverOrNot()==null||commodityOrder.getDeliverOrNot()==1)&&commodityOrder.getGoodsOrNot()==0){

        }
        return commodityOrderMapper.updateCommodityOrder(commodityOrder);
    }

    /**
     * 批量删除商品订单
     *
     * @param ids 需要删除的商品订单主键
     * @return 结果
     */
    @Override
    public int deleteCommodityOrderByIds(Long[] ids) {
        return commodityOrderMapper.deleteCommodityOrderByIds(ids);
    }

    /**
     * 删除商品订单信息
     *
     * @param id 商品订单主键
     * @return 结果
     */
    @Override
    public int deleteCommodityOrderById(Long id) {
        return commodityOrderMapper.deleteCommodityOrderById(id);
    }


    @Override
    public Map<String, Object> selectCommodityOrderBycargoUserId(Long cargoId) {
        Map<String, Object> map = new HashMap<>();
//        已取消的订单
        CommodityOrder commodityOrder = new CommodityOrder();
        commodityOrder.setCancelOrNot(1L);
        commodityOrder.setCargoUserId(cargoId);
        map.put("cancel", commodityOrderMapper.selectCommodityOrderBycargoUserId(commodityOrder));
//        未付款的订单
        commodityOrder.setPayOrNot(0L);
        commodityOrder.setCancelOrNot(0L);
        map.put("notPay", commodityOrderMapper.selectCommodityOrderBycargoUserId(commodityOrder));
//待收货的订单
        commodityOrder.setPayOrNot(1L);
        commodityOrder.setCancelOrNot(0L);
        commodityOrder.setGoodsOrNot(0L);
        map.put("unreceive", commodityOrderMapper.selectCommodityOrderBycargoUserId(commodityOrder));
//已收货的订单
        commodityOrder.setPayOrNot(1L);
        commodityOrder.setCancelOrNot(0L);
        commodityOrder.setGoodsOrNot(1L);
        map.put("receive", commodityOrderMapper.selectCommodityOrderBycargoUserId(commodityOrder));
        //全部订单
        commodityOrder.setPayOrNot(null);
        commodityOrder.setCancelOrNot(null);
        commodityOrder.setGoodsOrNot(null);
        map.put("allOrder", commodityOrderMapper.selectCommodityOrderBycargoUserId(commodityOrder));
        return map;
    }

    /**
     * 查询商品订单
     *
     * @param id 商品订单主键
     * @return 商品订单
     */
    @Override
    public CommodityOrder selectCommodityAllById(Long id) {
        CommodityOrder commodityOrder = new CommodityOrder();
        commodityOrder.setId(id);
        List<CommodityOrder> commodityOrders = commodityOrderMapper.selectCommodityOrderBycargoUserId(commodityOrder);

        return commodityOrders.get(0);
    }

    @Override
    public String remainder(Long id) {
        Object time = redisTemplate.opsForValue().get("commodityOrder" + id.toString());
        return time.toString();
    }

}
